package top.lixunda.ecommerce.server.good.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import top.lixunda.commerce.common.core.entity.MessageObject;
import top.lixunda.ecommerce.server.business.BaseController;
import top.lixunda.ecommerce.server.good.api.controller.ISimpleReviewPraiseController;
import top.lixunda.ecommerce.server.good.service.ISimpleReviewPraiseService;

import java.util.List;
import java.util.Map;

/**
 * @author Xumda
 * @version time:2020/1/14 16:16
 */
@Slf4j
@RestController
public class SimpleReviewPraiseController extends BaseController
        implements ISimpleReviewPraiseController {

    private final ISimpleReviewPraiseService simpleReviewPraiseService;

    @Autowired
    public SimpleReviewPraiseController(ISimpleReviewPraiseService simpleReviewPraiseService) {
        this.simpleReviewPraiseService = simpleReviewPraiseService;
    }

    /**
     * 根据简单评论的id获取当前评论的点赞数量
     *
     * @param id 评论id
     * @return 点赞次数
     */
    @Override
    public MessageObject<Integer> queryPraiseCountById(Integer id) {
        int count = simpleReviewPraiseService.queryPraiseCountById(id);
        return MessageObject.success("获取数据成功", count);
    }

    /**
     * 根据多个评论id获取多个评论的点赞次数
     *
     * @param ids 评论id的列表
     * @return 键值对，键为评论id，值为评论点赞次数
     */
    @Override
    public MessageObject<Map<Integer, Integer>> queryPraiseCountByIds(List<Integer> ids) {
        Map<Integer, Integer> countMap = simpleReviewPraiseService.queryPraiseCountByIds(ids);
        return MessageObject.success("获取数据成功", countMap);
    }

    /**
     * 对某个商品的某个简单评论进行一次点赞
     *
     * @param reviewId 评论id
     * @return 点赞后的该评论的点赞次数
     */
    @Override
    public MessageObject<Integer> addPraiseById(Integer reviewId) {
        int count = simpleReviewPraiseService.addPraiseById(reviewId);
        return MessageObject.success("评论成功", count);
    }

    /**
     * 取消对某个评论的点赞
     *
     * @param reviewId 评论id
     * @return 取消点赞后的该评论的点赞次数
     */
    @Override
    public MessageObject<Integer> cancelPraiseById(Integer reviewId) {
        int count = simpleReviewPraiseService.cancelPraiseById(reviewId);
        return MessageObject.success("取消评论成功", count);
    }

    /**
     * 当前登陆用户是否对某个评论进行点赞
     *
     * @param reviewId 评论id
     * @return 是否进行点赞
     */
    @Override
    public MessageObject<Boolean> currentPraiseFlagById(Integer reviewId) {
        Boolean flag = simpleReviewPraiseService.currentPraiseFlagById(reviewId);
        return MessageObject.success("获取数据成功", flag);
    }

    /**
     * 当前登陆用户是否对某些评论进行点赞
     *
     * @param reviewIds 评论id列表
     * @return 是否进行点赞，键值对，键为评论编号，值为是否点赞
     */
    @Override
    public MessageObject<Map<Integer, Boolean>> currentPraiseFlagByIds(List<Integer> reviewIds) {
        Map<Integer, Boolean> flag = simpleReviewPraiseService.currentPraiseFlagByIds(reviewIds);
        return MessageObject.success("获取数据成功", flag);
    }

}
